#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int n,m,k,T;

signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin >> n >> m;
	set<vector<int> > st1;
	vector<int> a(n + 2),b(n + 2);
	for(int i = 0;i < m; ++ i) {
		for(int i = 1;i <= n; ++ i) cin >> a[i];
		set<vector<int> > st2;
		for(auto &j : st1) {
			for(int i = 1;i <= n; ++ i) b[i] = j[a[i]];
			st2.insert(b);
		}
		for(auto &j : st2) st1.insert(j);
		st1.insert(a);
	}
	cout << st1.size();
	return 0;
}